from airtest.core.api import *
import datetime
from wechat_test.page.base_page import BasePage


class MePage(BasePage):

    def mepage(self):

        list_me = []
        sleep(2)
        for i in self.poco(type='android.widget.TextView', name='android.widget.TextView'):
            list_me.append(i.get_text())
        self.log_info(f'获取到我的页面列表:{list_me}')
        return list_me

    def anth(self):
        """
        1，如果存在退出登录按钮，点击退出登录
        2，点击登录-点击微信授权登录-同意并继续
        3，如果不存在退出登录，点击登录进入登录
        4,登录成功后获取用户id
        :return:userid
        """

        if self.poco(name="android.widget.TextView", text='退出登录').exists():
            self.log_info("用户已经登录--点击退出登录")
            self.poco(text='退出登录').click()
            self.log_info("点击登录")
            self.poco(text='点击登录').click()
            self.log_info("微信授权登录")
            self.poco(text='微信授权登录').click()
            self.log_info("点击同意并继续（坐标定位）")
            self.poco(name='com.tencent.mm:id/yq').click([0.7, 0.92])
            # self.poco.click([0.214, 0.845]])
            sleep(2)
        else:
            self.log_info("用户未登录--点击退出登录")
            self.poco(text='点击登录').click()
            sleep(1)
            self.log_info("微信授权登录")
            self.poco(text='微信授权登录').click()
            self.log_info("点击同意并继续（坐标定位）")
            self.poco(name='com.tencent.mm:id/yq').click([0.7, 0.92])
            sleep(2)
            self.log_info("开始遍历用户id信息")
        for i in [self.poco(name='android.widget.TextView', type='android.widget.TextView')][:6]:
            userid = i.get_text()
            self.log_info('登录成功')
        return userid

    # 签到领金币
    def sign(self):
        # 获取签到

        self.poco(text='签到领金币').click()
        if self.poco(text='今日已签到').exists():
            self.log_info('用户今天已经签到过了')
            # 点击我知道了，退出签到页面
            self.poco(name='android.widget.Button', type='android.widget.Button').click([0.5, 0.5])
        else:
            self.poco(text='签到成功').exists()
            self.log_info('用户今天签到成功')
            self.poco(name='android.widget.Button', type='android.widget.Button').click([0.5, 0.5])
        return self

    def my_money(self):
        # 获取个人页我的账户金额
        elenment = []
        for i in self.poco(name='android.widget.TextView', type='android.widget.TextView'):
            elenment.append(i)
        my_money = int(elenment[9].get_text())
        return my_money

    def recpage(self):
        if self.poco(text='点击登录').exists():
            self.log_info("用户未登录--点击退出登录")
            self.poco(text='点击登录').click()
            sleep(1)
            self.poco(text='微信授权登录').click()

            self.poco(name='com.tencent.mm:id/yq', type='android.view.ViewGroup').click([0.7, 0.9])
            sleep(2)
            self.poco(name='android.widget.TextView', text='立即开通').click()
        else:
            # 充值页面
            sleep(2)
            self.poco(name='android.widget.TextView', text='立即开通').click()

        # 需要点击屏幕加载view
        # self.poco(name='android.view.View', type='android.view.View').click([0.25, 0.9])
        sleep(1)
        # 根据坐标点击充值项
        self.poco(name='com.tencent.mm:id/yq', type="android.view.ViewGroup").click([0.7, 0.7])
        # 根据坐标点击协议
        self.poco(name='com.tencent.mm:id/yq', type="android.view.ViewGroup").click([0.1, 0.89])

        try:
            self.poco(text="立即购买").click()
        except:
            self.poco(name='com.tencent.mm:id/yq', type="android.view.ViewGroup").click([0.5, 0.8])
        # 加载支付键盘
        sleep(5)
        count = 1
        while count < 10:

            sleep(1)
            if self.poco(name='android.widget.ImageView', type="android.widget.ImageView").exists():
                self.log_info(f"循环查找{count}次查到")
                # self.poco(name='关闭', type='android.widget.Button').click()
                self.poco(name='android.widget.ImageView', type="android.widget.ImageView").click()
                break
            count += 1

        elements = []
        # 遍历充值弹框列表
        for i in self.poco(name='android.widget.TextView', type='android.widget.TextView'):
            text = i.get_text()
            elements.append(text)
        sleep(1)
        self.log_info(f"资费页文案：{elements}")
        return elements

    def rechargeRecord(self):
        '''
        充值记录
        :return:
        '''
        self.log_info('点击【充值记录】')
        self.poco(text='充值记录').click()
        element = []
        sleep(1)
        for i in self.poco(name='android.widget.TextView', type='android.widget.TextView'):
            element.append(i.get_text())
        self.log_info(f"充值记录页面元素:{element}")
        return element

    def h5Order(self, phone):
        if self.poco(text='点击登录').exists():
            self.log_info("用户未登录--点击退出登录")
            self.poco(text='点击登录').click()
            sleep(1)
            self.poco(text='微信授权登录').click()

            self.poco(name='com.tencent.mm:id/yq', type='android.view.ViewGroup').click([0.7, 0.9])
            sleep(2)
            self.poco(name='android.widget.TextView', text='立即充值').click()
        else:
            # 充值页面
            sleep(2)
            self.poco(name='android.widget.TextView', text='立即充值').click()

        self.poco(name='com.tencent.mm:id/yq').click([0.5, 0.6])
        sleep(4)
        # self.poco(name='phoneNumber').click()
        count = 0
        while count < 11:
            sleep(1)
            if self.poco(name='phoneNumber').exists():
                self.log_info(f"查找手机号输入框{count + 1}次")
                self.log_info("点击号码输入框")
                self.poco(name='phoneNumber').set_text(phone)
                self.log_info(f'输入手机号{phone}')
                break
            else:
                count += 1
                self.log_info(f"查找手机号输入框{count}次未找到")
        sleep(1)
        self.poco(text='立即领取').click()
        self.log_info("点击领取按钮")
        sleep(2)
        self.poco(name='com.tencent.mm:id/yq').click([0.3, 0.5])
        sleep(3)
        elements = []
        if self.poco(name='sms-sms-input').exists():
            self.log_info("拉起支付页成功")
            sleep(3)
            for i in self.poco(name='sms-okPay'):
                self.log_info(i.get_text())
                elements.append(i.get_text())
            self.poco(text='请输入验证码').click()
        else:
            self.log_error('找不确认支付按钮')
        self.log_info(f'按钮文案：{elements}')
        return elements

    def orderRecord(self):
        try:
            self.log_info('点击【消费记录】')
            self.poco(text='消费记录').wait(2).click()
        except:
            self.log_info('坐标点击【消费记录】')
            self.poco("com.tencent.mm:id/yq").click([0.5, 0.6])

        current_year = datetime.datetime.now().year
        self.log_info(f'当前年份{current_year}')
        time_year = int(current_year - 2025)
        self.log_info(f'滑动偏移量{time_year}')
        current_month = datetime.datetime.now().month
        self.log_info(f'当前月份{current_month}')
        time_num = int(current_month - 6)
        self.log_info(f'滑动偏移量{time_num}')


        if time_year == 0 :
            self.log_info(f'当前不用切换年份')
            try:
                self.poco(text='切换月份').click()
            except:
                self.log_info('坐标点击切换月份')
                self.poco("com.tencent.mm:id/yq").click([0.9, 0.04])

        elif time_year > 0 :
            try:
                self.log_info('切换年份')
                self.poco(text='切换月份').click()
            except:
                self.log_info('坐标点击切换月份')
                self.poco("com.tencent.mm:id/yq").click([0.9, 0.04])
            for i in range(time_year):
                self.poco.swipe([0.25, 0.79], [0.25, 0.84], duration=0.1)
            sleep(1)
            for i in range(6):
                self.poco.swipe([0.75, 0.8], [0.75, 0.83], duration=0.1)
            return time_num == 0

        elif time_year < 0 :
            try:
                self.log_info('切换年份')
                self.poco(text='切换月份').click()
            except:
                self.log_info('坐标点击切换月份')
                self.poco("com.tencent.mm:id/yq").click([0.9, 0.04])
            for i in range(abs(time_year)):
                self.poco.swipe([0.25, 0.84], [0.25, 0.79], duration=0.1)

        """处理月份"""
        sleep(2)
        if time_num > 0:
            for i in range(time_num):
                self.poco.swipe([0.75, 0.8], [0.75, 0.83], duration=0.1)
            sleep(2)
            self.poco("com.tencent.mm:id/yq").click([0.91, 0.54])
        elif time_num == 0:
            sleep(2)
            element = []
            for i in self.poco(name='android.widget.TextView', type='android.widget.TextView'):
                element.append(i.get_text())
            self.log_info(f"页面元素:{element}")
            return element
        elif time_num < 0:
            self.log_info('滑动月份到6月')
            for i in range(abs(time_num)):
                self.poco.swipe([0.75, 0.83], [0.75, 0.8], duration=0.1)

            sleep(2)
            self.log_info('点击确定')
            self.poco("com.tencent.mm:id/yq").click([0.91, 0.54])
        count = 0
        element = []
        while count < 3:
            self.poco("com.tencent.mm:id/yq").click()
            sleep(1)
            if self.poco(text='逍遥小神医').exists():
                self.log_info(f"点击了:{count + 1}次")
                for i in self.poco(name='android.widget.TextView', type='android.widget.TextView').wait(2):
                    sleep(0.5)
                    element.append(i.get_text())
                self.log_info(f"页面元素:{element}")
                break
            else:
                count += 1
                self.log_info(f"点击了:{count}次")

        return element
